#include "CCamera.h"
#include <stdio.h>
CCamera::CCamera()
{
	m_nPosX = 0;
	m_nPosY = 0;

	int width = 150;
	int height = 150;

	m_rScreenBound.left = 320 - width/2;
	m_rScreenBound.top = 220 - height/2;

	m_rScreenBound.right = m_rScreenBound.left + width;
	m_rScreenBound.bottom = m_rScreenBound.top + height;
}

void CCamera::MoveCamera(RECT rWorld)
{
	// traslate world rect to screen space

	char buffer[128];

	sprintf_s(buffer,_countof(buffer),"CX: %d CY: %d WX: %d WY: %d\n", m_nPosX, m_nPosY, rWorld.left, rWorld.top);
	OutputDebugString(buffer);
		
	RECT screen = rWorld;

	screen.left = rWorld.left - m_nPosX;
	screen.right = rWorld.right - m_nPosX;

	screen.top = rWorld.top - m_nPosY;
	screen.bottom = rWorld.bottom - m_nPosY;

	// then check if it goes out x bound
	if(screen.left < m_rScreenBound.left)
		m_nPosX -= m_rScreenBound.left - screen.left ;
	else if(screen.right > m_rScreenBound.right)
		m_nPosX += screen.right - m_rScreenBound.right;

	// then check if it goes out y bound
	if(screen.top < m_rScreenBound.top)
		m_nPosY -=  m_rScreenBound.top - screen.top;
		

	else if(screen.bottom > m_rScreenBound.bottom)
		m_nPosY += screen.bottom - m_rScreenBound.bottom;
}


CCamera* CCamera::GetInstance()
{
	static CCamera instance;
	return &instance;
}